package com.wf.course.web.controller.course;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.utils.page.PageBean;
import com.wf.course.model.ad.CourseCategory;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.model.course.Speaker;
import com.wf.course.model.goods.GoodsInfo;
import com.wf.course.service.ICourseCategoryService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IGoodsInfoService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 课程列表Controller
 */
@RequestMapping("courseInfo")
@Controller
public class CourseInfoController {

	@Autowired
	private ICourseInfoService courseService;
	@Autowired
	private IGoodsInfoService goodsService;
	@Autowired
	private ICourseCategoryService courseCategoryService;

	/**
	 * 课程列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param courseName
	 * @param speakerId
	 * @param model
	 * @return
	 */
	@LogInfo("查询课程信息列表")
	@RequestMapping("courseList")
	public String courseList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "courseName", required = false) String courseName, 
			@RequestParam(value = "speakerId", required = false) String speakerId, 
			HttpSession httpSession,
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("courseName", courseName);
		cons.put("speakerId", speakerId);
		PageBean<CourseInfo> pageBean = new PageBean<CourseInfo>(pageNo, pageSize, null, cons);
		pageBean.setUrl("courseInfo/courseList");
		this.courseService.indexInfo(pageBean);
		model.addAttribute("page", pageBean);
		
		httpSession.setAttribute("page", pageBean);
		
		 List<Speaker> speakers = courseService.querySpeaker();
		 model.addAttribute("speakers",speakers);
		
		return "course/courseInfoList";
	}

	/**
	 * 课程 修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("新增或修改课程信息，页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model,HttpSession httpSession) {
		 
		if (id != null && id != 0) {
			CourseInfo CourseInfo = courseService.queryCourseInfoById(id);
			model.addAttribute("CourseInfo", CourseInfo);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 List<Speaker> speakers = courseService.querySpeaker();
		 model.addAttribute("speakers",speakers);
		 
		 List<GoodsInfo> goods = goodsService.queryGoods();
		 model.addAttribute("goods",goods);
		 
		 List<CourseCategory> coures = courseCategoryService.getCourseCategoryAllList();
		 model.addAttribute("coures",coures);
		
		return "course/courseInfoEdit";
	}
	
	/**
	 * 课程置顶
	 * 
	 * @param id
	 * @return
	 */
	@LogInfo("课程置顶")
	@RequestMapping("CourseInfoTop/{id}")
	public String CourseInfoTop(@PathVariable("id") Integer id) {
		 
		if (id != null && id != 0) {
			 courseService.CourseInfoTop(id);
		}
		
		return "redirect:/courseInfo/courseList";
	}

	/**
	 * 课程新增和修改
	 * 
	 * @param CourseInfo
	 * @param imgFile
	 * @param h5File
	 * @param request
	 * @return
	 */
	@LogInfo("新增或修改课程信息")
	@RequestMapping(value = "courseInfoEdit" , produces = "text/html;charset=UTF-8")
	public String courseInfoEdit(@ModelAttribute CourseInfo CourseInfo, 
			@RequestParam(value = "courseFile", required = false)MultipartFile courseFile,
 			HttpServletRequest request,HttpSession httpSession,Model model) {		
 		courseService.saveOrUpdateCourseInfo(CourseInfo,courseFile);
 		
 		//保存后，跳转到之前的列表页
		@SuppressWarnings("unchecked")
		PageBean<T> page =  (PageBean<T>) httpSession.getAttribute("page");
		 
		PageBean<CourseInfo> pageBean = new PageBean<CourseInfo>(page.getActulPageNo(), page.getPageSize(), null, page.getConMap());
		pageBean.setUrl("courseInfo/courseList");
		
		this.courseService.indexInfo(pageBean);
		 model.addAttribute("page", pageBean);
		
		 List<CourseInfo> courses = courseService.queryCourses();
		 model.addAttribute("courses",courses);
		
		 List<Speaker> speakers = courseService.querySpeaker();
		 model.addAttribute("speakers",speakers);
		
		return "course/courseInfoList";
 		
		
		 
	}

	/**
	 * 课程删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除课程信息")
	@RequestMapping(value = "remove/{id}")
	public String removeCourseInfo(@PathVariable("id") Integer id) {
		
		courseService.deleteById(id);
		
		return "redirect:/courseInfo/courseList";
	}
	

}
